<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员收货地址ajax类
 * @package  ajax/comm/customer
 * @author   allen.qiang
 */

quanli_require('module/base/customer/custDeliver.class.php');

class ajax_deliver {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //新增/修改收货地址
                    $this->deliverEdit();
                    break;

                case 2: //删除收货地址
                    $this->deliverDelete();
                    break;

                case 3: //列表
                    $this->deliverList();
                    break;

                case 4: //详情
                    $this->deliverDetail();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 会员收货地址列表
     */
    private function deliverList() {

        if (!isset($_REQUEST['pcustID']) || empty($_REQUEST['pcustID'])) {
            returnJsonData('参数错误');
        }

        //实例会员类
        $custDeliver = new custDeliver();

        //查询会员收货地址列表
        $deliver_array = $custDeliver->searchCustDeliver( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($deliver_array) {

            foreach($deliver_array as &$deliver) {

                $deliverID = $deliver['deliverID'];
                $address = $deliver['provinceName'] . $deliver['cityName'] . $deliver['countyName'] . $deliver['address'];

                $result_array['rows'][] = array (
                    'id' => $deliverID,
                    'cell' => array(
                        '<a href="javascript: void(0);" onclick="deliver.edit(' . $deliverID . ');">修改</a>&nbsp;&nbsp;'.
                        '<a href="javascript: void(0);" onclick="deliver.del(' . $deliverID . ');">删除</a>',
                        $deliver['custName'],
                        $address,
                        $deliver['mobile'],
                        $deliver['phone']
                    )
                );

            }

        }

        echo json_encode($result_array);

    }

    /**
     * 会员收货地址详情
     */
    private function deliverDetail() {

        if (!isset($_REQUEST['pcustID']) || empty($_REQUEST['pcustID'])) {
            returnJsonData('参数错误');
        }
        if (!isset($_REQUEST['deliverID']) || empty($_REQUEST['deliverID'])) {
            returnJsonData('参数错误');
        }

        //实例会员类
        $custDeliver = new custDeliver();

        //查询会员收货地址列表
        $deliver_array = $custDeliver->searchCustDeliverDetail( $_REQUEST['deliverID'], $_REQUEST['pcustID'] );
        if ($deliver_array) {
            returnJsonData('ok', 200, $deliver_array);
        } else {
            returnJsonData('会员收货地址没有找到', 404);
        }

    }


    /**
     * 添加或修改会员收货地址
     */
    private function deliverEdit() {

        global $G_PGLOB;

        if (!isset($_REQUEST['pcustID']) || !is_numeric($_REQUEST['pcustID']))
            die( json_encode( array( 'code' => 406, 'message' => '参数错误' ) ) );

        //地址类型，type为2的时候为自提
        if(isset($_REQUEST['type']) && $_REQUEST['type'] == 2){

            //验证配送站参数
            if(!isset($_REQUEST['distribution_id']) || empty($_REQUEST['distribution_id'])){
                returnJsonData('自提网店必须选择');
            }

            $distribution_id = $_REQUEST['distribution_id'];

            //包含配送站类
            quanli_require('module/base/system/sysDistribution.class.php');

            //实例化配送站类
            $sysDistribution = new sysDistribution();

            //查询配送站信息
            $arg_array = array(
                'distribution_id' => $distribution_id
            );

            $distribution_array = $sysDistribution->search( $arg_array );

            $_REQUEST['provinceid'] = $distribution_array[0]['provinceid'];
            $_REQUEST['cityid'] = $distribution_array[0]['cityid'];
            $_REQUEST['countyid'] = $distribution_array[0]['countyid'];
            $_REQUEST['address'] = $distribution_array[0]['address'];

        }

        //实例化会员类
        $custDeliver = new custDeliver();

        //保存地址信息
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $result_array = $custDeliver->editCustDeliver( $_REQUEST );

        echo json_encode($result_array);

    }


    /**
     * 删除收货地址
     */
    private function deliverDelete() {

        global $G_PGLOB;

        if (!isset($_REQUEST['pcustID']) || !is_numeric($_REQUEST['pcustID']))
            die( json_encode( array( 'code' => 406, 'message' => '参数错误' ) ) );

        if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id']))
            die( json_encode( array( 'code' => 400, 'message' => '缺少参数' ) ) );

        //实例会员地址类
        $custDeliver = new custDeliver( $G_PGLOB->DBCA );

        //删除会员收货地址
        $result_array = $custDeliver->delCustDeliver($_REQUEST['pcustID'], $_REQUEST['id']);

        echo json_encode( $result_array );

    }

}